#include "pch.h"
#include "queue.h"
#include <cstdlib>

bool CreateQueue(Queue &Q) {
	Q = (Queue)malloc(sizeof(struct QNode));
	if (Q == NULL) exit(-1);
	Q->Data = (ElemType *)malloc(InitQueueSize * sizeof(ElemType));
	if (Q->Data == NULL) exit(-1);
	Q->Front = Q->Rear = 0;
	Q->QueueSize = InitQueueSize;
	return true;
}

inline bool IsFull(Queue Q) {
	return ((Q->Rear + 1) % Q->QueueSize == Q->Front);
}

inline bool IsEmpty(Queue Q) {
	return (Q->Front == Q->Rear);
}

bool AddQ(Queue &Q, ElemType X) {
	if (IsFull(Q)) {
		Q->Data = (ElemType *)realloc(Q->Data, (Q->QueueSize + QueueIncrease) * sizeof(ElemType));
		Q->QueueSize += QueueIncrease;
		return true;
	}
	else {
		Q->Rear = (Q->Rear + 1) % Q->QueueSize;
		Q->Data[Q->Rear] = X;
		return true;
	}
}

bool GetFront(Queue Q, ElemType &e) {
	if (!IsEmpty(Q)) {
		e = Q->Data[(Q->Front + 1) % Q->QueueSize];
		return true;
	}
	return false;
}

bool DeleteQ(Queue &Q) {
	if (IsEmpty(Q)) {
		return false;
	}
	else {
		Q->Front = (Q->Front + 1) % Q->QueueSize;
		return true;
	}
}